package bigapps.myneighbor.controller.rest;

import java.awt.PageAttributes.MediaType;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.transaction.annotation.Transactional;

import bigapps.myneighbor.domain.ParkingFacility;
import bigapps.myneighbor.domain.ParkingFacilityList;
import bigapps.myneighbor.service.ParkingFacilityService;

@Controller
public class ParkingFacilityRESTController {
	@Autowired
	public ParkingFacilityService parkingFacilityService;

      public ParkingFacilityService getParkingFacilityService(){
        return this.parkingFacilityService;
      }

      public void setParkingFacilityService(ParkingFacilityService parkingFacilityService){
        this.parkingFacilityService = parkingFacilityService;
      }

	@RequestMapping(value = "/json/parking_facility/{latitude},{longitude}", method=RequestMethod.GET, headers="Accept=application/json, text/html, */*")
	@PayloadRoot(localPart="ParkingFacilityList", namespace="http://test.com")
//	@Produces(MediaType.APPLICATION_JSON)
//      @Transactional
	public @ResponseBody ParkingFacilityList getParkingFacilityListJSON(@PathVariable("latitude") Double latitude, @PathVariable("longitude") Double longitude){
		System.out.println("latitude: " + latitude + ", longitude: " + longitude);
		System.out.println("json");
		return getParkingFacilityList(latitude, longitude);
	}

	@RequestMapping(value = "/xml/parking_facility/{latitude},{longitude}", method=RequestMethod.GET, headers="Accept=application/xml, text/html, */*")
//	@Produces(MediaType.APPLICATION_XML)
	@PayloadRoot(localPart="ParkingFacilityList", namespace="http://test.com")
//      @Transactional
	public @ResponseBody ParkingFacilityList getParkingFacilityListXML(@PathVariable("latitude") Double latitude, @PathVariable("longitude") Double longitude){
		System.out.println("latitude: " + latitude + ", longitude: " + longitude);
		System.out.println("xml");
		return getParkingFacilityList(latitude, longitude);
		//, headers="Accept=application/xml, application/json, text/html, */*"
	}
/*
	@PayloadRoot(localPart="ParkingFacility", namespace="http://test.com")
	@RequestMapping(value = "rest2", method=RequestMethod.GET, headers="Accept=application/xml, application/json, text/html")
//	@Produces(MediaType.APPLICATION_XML)
//      @Transactional
	public @ResponseBody ParkingFacility getClosesParkingFacilityXML(){
		Float latitude = new Float(5.0);
		Float longitude = new Float(5.0);
		System.out.println("latitude: " + latitude + ", longitude: " + longitude);
		System.out.println("xml");
		return getParkingFacilityList(latitude, longitude).parkingFacilityList.get(0);
	}
*/
	private ParkingFacilityList getParkingFacilityList(double latitude, double longitude)
	{
		try{
			System.out.println("getParkingFacilityList");
			return parkingFacilityService.getParkingFacilityList(latitude, longitude);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
}
